package utils;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.xuegengwang.xuegengwang.R;
import com.google.gson.Gson;

import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.tencent.qzone.QZone;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;
import cn.sharesdk.wechat.utils.WechatHelper;
import common.Constant;
import https.InterNetController;
import web.bean.ShareBean;

/**
 * Created by Administrator on 2017/4/19.
 * 分享对话框
 */

public class ShowShareDialog extends Dialog implements View.OnClickListener {

    private Context context;
    private View view;
    private TextView share_cancel;
    private LinearLayout share_weixin;
    private LinearLayout share_friends;
    private LinearLayout share_qq;
    private LinearLayout share_qzone;
    private LinearLayout share_weibo;

    private String content;
    private String image;
    private String title;
    private String url;
    private PlatformActionListener platformActionListener;
    private OnShareClickListener onShareClickListener;

    public void setOnShareClickListener(OnShareClickListener onShareClickListener) {
        this.onShareClickListener = onShareClickListener;
    }

    public void setPlatformActionListener(PlatformActionListener platformActionListener) {
        this.platformActionListener = platformActionListener;
    }

    public ShowShareDialog(Context context) {
        super(context);
        this.context = context;
    }

    public ShowShareDialog(Context context, int themeResId) {
        super(context, themeResId);
        this.context = context;
    }

    protected ShowShareDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        this.context = context;
    }


    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        view = LayoutInflater.from(context).inflate(R.layout.sharedialog, null);
        setContentView(view);
        Window dialogWindow = getWindow();
        dialogWindow.setGravity(Gravity.LEFT | Gravity.BOTTOM);//设置对话框位置
        initView();
    }

    private void initView() {
        share_cancel = (TextView) view.findViewById(R.id.share_cancel);
        share_weixin = (LinearLayout) view.findViewById(R.id.share_weixin);
        share_friends = (LinearLayout) view.findViewById(R.id.share_friends);
        share_qq = (LinearLayout) view.findViewById(R.id.share_qq);
        share_qzone = (LinearLayout) view.findViewById(R.id.share_qzone);
        share_weibo = (LinearLayout) view.findViewById(R.id.share_weibo);
        share_cancel.setOnClickListener(this);
        share_weixin.setOnClickListener(this);
        share_friends.setOnClickListener(this);
        share_qq.setOnClickListener(this);
        share_qzone.setOnClickListener(this);
        share_weibo.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.share_cancel:
                ShowShareDialog.this.dismiss();
                break;
            case R.id.share_weixin:
                if(onShareClickListener!=null){
                    onShareClickListener.onShareClicklistener();
                }
                WechatHelper.ShareParams Wechatsp = new Wechat.ShareParams();
                Wechatsp.title = title;
                Wechatsp.text = content;
                Wechatsp.imageUrl = image;
                Wechatsp.url = url;
                Wechatsp.setShareType(Platform.SHARE_WEBPAGE);
                Platform Wechatplatform = ShareSDK.getPlatform(Wechat.NAME);
                Wechatplatform.share(Wechatsp);
                Wechatplatform.setPlatformActionListener(new PlatformActionListener() {
                    @Override
                    public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                        ToastUtils.shortToast("分享到微信成功");
                        if(platformActionListener!=null){
                            platformActionListener.onComplete(platform,i,hashMap);
                        }
                        ShowShareDialog.this.dismiss();
                    }

                    @Override
                    public void onError(Platform platform, int i, Throwable throwable) {
                        if(platformActionListener!=null){
                            platformActionListener.onError(platform,i,throwable);
                        }
                        ToastUtils.shortToast("分享失败");
                    }

                    @Override
                    public void onCancel(Platform platform, int i) {
                        ToastUtils.shortToast("取消分享");
                        if(platformActionListener!=null){
                            platformActionListener.onCancel(platform,i);
                        }
                        ShowShareDialog.this.dismiss();
                    }
                });
                break;
            case R.id.share_friends:
                if(onShareClickListener!=null){
                    onShareClickListener.onShareClicklistener();
                }
                WechatMoments.ShareParams WechatMomentsp = new WechatMoments.ShareParams();
                WechatMomentsp.title = title;
                WechatMomentsp.text = content;
                WechatMomentsp.imageUrl = image;
                WechatMomentsp.url = url;
                WechatMomentsp.setShareType(Platform.SHARE_WEBPAGE);
                Platform WechatMomentplatform = ShareSDK.getPlatform(WechatMoments.NAME);
                WechatMomentplatform.share(WechatMomentsp);
                WechatMomentplatform.setPlatformActionListener(new PlatformActionListener() {
                    @Override
                    public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                        ToastUtils.shortToast("分享到朋友圈成功");
                        if(platformActionListener!=null){
                            platformActionListener.onComplete(platform,i,hashMap);
                        }
                        ShowShareDialog.this.dismiss();
                    }

                    @Override
                    public void onError(Platform platform, int i, Throwable throwable) {
                        if(platformActionListener!=null){
                            platformActionListener.onError(platform,i,throwable);
                        }
                        ToastUtils.shortToast("分享失败");
                    }

                    @Override
                    public void onCancel(Platform platform, int i) {
                        ToastUtils.shortToast("取消分享");
                        if(platformActionListener!=null){
                            platformActionListener.onCancel(platform,i);
                        }
                        ShowShareDialog.this.dismiss();
                    }
                });
                break;
            case R.id.share_qq:
                if(onShareClickListener!=null){
                    onShareClickListener.onShareClicklistener();
                }
                QQ.ShareParams QQsp = new QQ.ShareParams();
                QQsp.title = title;
                QQsp.text = content;
                QQsp.imageUrl = image;
                QQsp.titleUrl = url;
                Platform qqplatform = ShareSDK.getPlatform(QQ.NAME);
                qqplatform.share(QQsp);
                qqplatform.setPlatformActionListener(new PlatformActionListener() {
                    @Override
                    public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                        ToastUtils.shortToast("分享到QQ成功");
                        if(platformActionListener!=null){
                            platformActionListener.onComplete(platform,i,hashMap);
                        }
                        ShowShareDialog.this.dismiss();
                    }

                    @Override
                    public void onError(Platform platform, int i, Throwable throwable) {
                        if(platformActionListener!=null){
                            platformActionListener.onError(platform,i,throwable);
                        }
                        ToastUtils.shortToast("分享失败");
                    }

                    @Override
                    public void onCancel(Platform platform, int i) {
                        ToastUtils.shortToast("取消分享");
                        if(platformActionListener!=null){
                            platformActionListener.onCancel(platform,i);
                        }
                        ShowShareDialog.this.dismiss();
                    }
                });
                break;
            case R.id.share_qzone:
                if(onShareClickListener!=null){
                    onShareClickListener.onShareClicklistener();
                }
                QZone.ShareParams qqzonesp = new QZone.ShareParams();
                qqzonesp.title = title;
                qqzonesp.text = content;
                qqzonesp.imageUrl = image;
                qqzonesp.titleUrl = url;
                Platform qqzoneplatform = ShareSDK.getPlatform(QZone.NAME);
                qqzoneplatform.setPlatformActionListener(new PlatformActionListener() {
                    @Override
                    public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                        ToastUtils.shortToast("分享到QQ空间成功");
                        if(platformActionListener!=null){
                            platformActionListener.onComplete(platform,i,hashMap);
                        }
                        ShowShareDialog.this.dismiss();
                    }

                    @Override
                    public void onError(Platform platform, int i, Throwable throwable) {
                        if(platformActionListener!=null){
                            platformActionListener.onError(platform,i,throwable);
                        }
                        ToastUtils.shortToast("分享失败");
                    }

                    @Override
                    public void onCancel(Platform platform, int i) {
                        ToastUtils.shortToast("取消分享");
                        if(platformActionListener!=null){
                            platformActionListener.onCancel(platform,i);
                        }
                        ShowShareDialog.this.dismiss();
                    }
                });
                qqzoneplatform.share(qqzonesp);
                break;
            case R.id.share_weibo:
                if(onShareClickListener!=null){
                    onShareClickListener.onShareClicklistener();
                }
                SinaWeibo.ShareParams SinaWeibosp = new SinaWeibo.ShareParams();
                SinaWeibosp.setText(content);
                SinaWeibosp.setImagePath(image);
                SinaWeibosp.setUrl(url);
                SinaWeibosp.setTitle(title);
                Platform weibo = ShareSDK.getPlatform(SinaWeibo.NAME);
                weibo.setPlatformActionListener(new PlatformActionListener() {
                    @Override
                    public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                        ToastUtils.shortToast("分享到微博成功");
                        if(platformActionListener!=null){
                            platformActionListener.onComplete(platform,i,hashMap);
                        }
                        ShowShareDialog.this.dismiss();
                    }

                    @Override
                    public void onError(Platform platform, int i, Throwable throwable) {
                        if(platformActionListener!=null){
                            platformActionListener.onError(platform,i,throwable);
                        }
                        ToastUtils.shortToast("分享失败");
                    }

                    @Override
                    public void onCancel(Platform platform, int i) {
                        ToastUtils.shortToast("取消分享");
                        if(platformActionListener!=null){
                            platformActionListener.onCancel(platform,i);
                        }
                        ShowShareDialog.this.dismiss();
                    }
                }); // 设置分享事件回调
                weibo.share(SinaWeibosp);
                break;
            default:
                break;
        }
    }

    public interface OnShareClickListener{
        void onShareClicklistener();
    }

}
